package com.controller;


import com.entity.BUser;
import com.mapper.BUserMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    BUserMapper bUserMapper;
    @GetMapping("/login")
    public String toLoginPage(){

        return "back/login";
    }
    @GetMapping("/welcome")
    public String welcome(){

        return "back/welcome";
    }
    @PostMapping("/toLogin")
    public String login(BUser admin, HttpSession httpSession, Model model) {
        BUser admin1 = bUserMapper.userLogin(admin);
        if (admin1!=null){
            httpSession.setAttribute("admin",admin1);
            model.addAttribute("admin",admin1);
//            return "redirect:/admin";
            return "back/index";
        }
        else {
            model.addAttribute("msg","账号或密码错误，请重试！");
            return "back/login";
        }
    }
}
